#include <iostream>
#include "igslib_iutil.h"
#include "igslib_iserial_manager.h"
#include "igslib_iacceptor.h"
#include "igslib_iacceptor.h"

namespace igslib
{
    namespace acceptor_sample
    {
        /*!
         * This sample demostrates manipulating the acceptor.
         */
        static void show_value()
        {
            IGPIO* gpio = nullptr;
            ISpi* spi = nullptr;
            IAcceptor* acceptor = nullptr;

            {
                ISerialManager* mgr = GetSerialManagerInstance();
                spi = mgr->New_ProjMCU_spi("DragonBallRace");	
                spi->SetWorkingFrequency(10000000);
                bool open_result = spi->Open(ISpi::cs0);
                if (open_result == false)
                {
                    throw_igslib_exception("SPI open fail.");
                }
            }

            {
                IGPIOManager* mgr = GetGPIOManagerInstance();
                gpio = mgr->New_ProjMCU("DragonBallRace");
                gpio->SetSPI(spi);
				gpio->SetMode(IGPIO::mode::input);
				gpio->SetNumber(5);
            }

            {
                IAcceptorManager* mgr = GetAcceptorManagerInstance();
                acceptor = mgr->New_ProjMCU("DragonBallRace");
                acceptor->SetSPI(spi);
                acceptor->SetGPIO(gpio,NULL);
                acceptor->SetConfirmRange(10,150);
                acceptor->SetJammedTime(160);
            }

            auto last = acceptor->GetValue();
            while(util::KbHit() != 27)
            {
                auto now = acceptor->GetValue();
                if (now != last)
                {
                    util::Log("%s> acceptor's value=%g,error=%d\n",__func__,now,acceptor->GetErrorState());
                }
                last = now;
                spi->Update();
                util::SleepMSTime(16);
            }
            exit(EXIT_SUCCESS);
        }
    };
};
